package com.gdziarmaga.webshop.web.menus;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ActionEvent;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gdziarmaga.webshop.book.model.Book;
import com.gdziarmaga.webshop.book.services.BookManager;
import com.gdziarmaga.webshop.review.model.Review;
import com.gdziarmaga.webshop.review.services.ReviewManager;
import com.gdziarmaga.webshop.utils.filtering.BaseFilter;
import com.gdziarmaga.webshop.utils.filtering.FilterComparisonType;
import com.gdziarmaga.webshop.utils.filtering.FilterField;
import com.gdziarmaga.webshop.utils.springcontext.AppContext;
import com.gdziarmaga.webshop.web.managedBeans.client.ClientLoginBean;
import com.gdziarmaga.webshop.web.utils.FacesUtils;

@ManagedBean(name = "bookTableBean")
@RequestScoped
@Transactional(propagation = Propagation.REQUIRED)
public class BookTableBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8323276478965539604L;

	private BookManager bookManager;
	private ReviewManager reviewManager;

	private Review review;

	public void addReview(ActionEvent event) {

		ClientLoginBean bean = ((ClientLoginBean) FacesUtils.getRequest()
				.getSession().getAttribute("clientLoginBean"));
		review.setNick(bean.getLogin());
		review.setBook(getSelectedBook());
		review.setReview_date(new Date());
		reviewManager.saveReview(review);
	}

	public Review getReview() {
		if (review == null) {
			review = new Review();
		}
		return review;
	}

	public void setReview(Review review) {
		this.review = review;
	}

	private Book selectedBook;

	public ReviewManager getReviewManager() {
		return reviewManager;
	}

	public void setReviewManager(ReviewManager reviewManager) {
		this.reviewManager = reviewManager;
	}

	private String navigate;

	public BookTableBean() {
	}

	@PostConstruct
	public void init() {
		setBookManager((BookManager) AppContext.getApplicationContext()
				.getBean("bookManager"));
		setReviewManager((ReviewManager) AppContext.getApplicationContext()
				.getBean("reviewManager"));
	}

	public Book getSelectedBook() {
		if (selectedBook == null) {
			Long id = (Long) FacesUtils.flashScope().get("selectedBookId");
			selectedBook = bookManager.getBookById(id);
			FacesUtils.flashScope().put("selectedBookId", id);

		}
		return selectedBook;
	}

	public List<Review> getReviewList() {
		BaseFilter baseFilter = new BaseFilter();
		baseFilter.addFilterField(new FilterField<Book>(selectedBook, "book",
				FilterComparisonType.EQUAL));
		return reviewManager.getReviewsByFilterObject(baseFilter);
	}

	public void choiceBook(ActionEvent event) {
		Long id = (Long) event.getComponent().getAttributes()
				.get("selectedBookId");
		FacesUtils.flashScope().put("selectedBookId", id);
		// selectedBook = bookManager.getBookById(id);
		// navigate = "mainContent/bookPage";
		return;
	}

	public void setBookManager(BookManager bookManager) {
		this.bookManager = bookManager;
	}

	public BookManager getBookManager() {
		return bookManager;
	}

	public String navigate() {
		return navigate;
	}

}